import request from "@/core/request";

// 手机otp登錄
export function loginBySmsOtp(data: InternalAuth.LoginBySmsOtpForm) {
  return request<InternalToken.Token>({
    url: "/token/sms-otp",
    method: "POST",
    ignoreAuth: true,
    data,
    silentError: true,
    throwError: false,
  });
}

// 手機號與登入密碼登錄
export function loginByPassword(data: InternalAuth.LoginByPassword) {
  return request<InternalToken.Token>({
    url: "/token/password",
    method: "POST",
    ignoreAuth: true,
    data,
  });
}

// 刷新token
export async function refreshToken(data: InternalAuth.RefreshTokenForm) {
  const res = await request<InternalToken.Token>({
    url: "/token",
    method: "PUT",
    ignoreAuth: true,
    data,
  });
  if (res) {
    setToken('PUT')
  }
  return res
}

// 登出
export async function logout() {
  const res = await request({ url: "/token", method: "DELETE" });
  if (res) {
    setToken('DELETER')
  }
  return res
}


function setToken(type: string, token?: InternalToken.Token) {
  switch (type) {
    case "DELETER":
      console.log('DELETER');
      return
    case "PUT":
      console.log('PUT', token);
      return
    case "ADD":
      console.log('ADD', token);
  }
}